package zdc.display {
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.utils.clearTimeout;
import flash.utils.setTimeout;

/**
 *
 * @author Simple Slime
 */
public class SeqMovieClipLoader extends SeqMovieClip {
    public static const CONTENT_COMPLETE:String = "SeqMovieClipLoader_CONTENT_COMPLETE";
    private var autoLoad:Boolean = true;
    private var path:String;
    private var compl:Boolean = false;
    private var tempAutoSetup:Boolean = true;
    private var swf:MovieClip;
    private var loader:Loader;
    private var delayTime:Number;
    private var timeout:uint = 0;

    public function SeqMovieClipLoader(path:String, preview:DisplayObject = null, gesture:DisplayObject = null, rec:Boolean = true, delayTime:Number = 0, autoLoad:Boolean = true, autoSetup:Boolean = true) {
        this.path = path;
        this.autoLoad = autoLoad;
        this.tempAutoSetup = autoSetup;
        this.delayTime = delayTime;
        recur = rec;

        super(null, preview, gesture, rec, false);
        addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);

        initPreVars();
        stage ? init() : addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        initPostVars();
        initStates();
        initEvents();
        run();
    }

    private function initPreVars():void {

    }

    private function initPostVars():void {

    }

    private function initStates():void {

    }

    private function initEvents():void {

    }

    private function run():void {
        if (autoLoad) {
            if (delayTime) {										//trace("delayTime:", delayTime);
                timeout = setTimeout(load, delayTime);
            }
            else {
                load();
            }
        }
    }

    private function onRemoved(e:Event):void {
        removeEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
        clearTimeout(timeout);
        if (loader) {
            loader.unloadAndStop();
        }
    }

    /**
     * 开始加载
     */
    public function load():void {
        clearTimeout(timeout);

        if (compl) {
            return;
        }

        loader = new Loader();
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);				//trace("path:", path);
        loader.load(new URLRequest(path));
    }

    private function onLoaderComplete(e:Event):void {
        var info:LoaderInfo = e.currentTarget as LoaderInfo;
        info.removeEventListener(Event.COMPLETE, onLoaderComplete);
        swf = info.content as MovieClip;
        compl = true;
        addChild(swf);
        target = swf;

        if (tempAutoSetup) {		//trace("onLoaderComplete setupActor");
            setupActor();
        }

        gesture.x = stage.stageWidth - gesture.width >> 1;
        dispatchEvent(new Event(SeqMovieClipLoader.CONTENT_COMPLETE));
    }

    /**
     * 加载的swf
     */
    public function get rawContent():MovieClip {
        return swf;
    }

}

}